package
{
    import com.ultizen.farm.core.Application;
    import com.ultizen.farm.core.PreloaderView;
    import com.ultizen.farm.logging.Logger;
    import com.ultizen.farm.logging.TraceHandler;
    import com.ultizen.farm.preloader.PreloaderUI;
    import com.ultizen.farm.utils.loadEmbeddedXML;
    
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
  
    public class Preloader extends Application
    {
        [Embed(source="Config.config", mimeType="application/octet-stream")]
        private static const CONFIG:Class;

        // percentage of total loading that the swf accounts for
        public static const PERCENT_OFFSET:Number = 0.50;

        private var _preloaderView:PreloaderUI;

        /**
         * Constructor
         * 
         */        
        public function Preloader()
        {
            initializeStage();
            loadApplication(loadEmbeddedXML(CONFIG));     
            initializeLogging();  
        }


        /**
         * Set all stage properties here
         * 
         */        
        private function initializeStage():void
        {
            this.stage.align = StageAlign.TOP_LEFT;
            this.stage.scaleMode = StageScaleMode.NO_SCALE;
            //this.stage.showDefaultContextMenu = false;
            stage.tabChildren = false;
        }   
        private function initializeLogging():void
        {
            Logger.instance.addLogger(new TraceHandler(TraceHandler.LOG_LEVEL +
                                                       " : " +
                                                       TraceHandler.MESSAGE));
        }
        override protected function onApplicationLoadInit():void
        {
            _preloaderView = new PreloaderUI();
            addChild(_preloaderView);
        }
      
        override protected function onApplicationLoadProgress(bytesLoaded:int,
                                                              bytesTotal:int):void
        {
            var percent:Number = bytesLoaded / bytesTotal * PERCENT_OFFSET;
            
            _preloaderView.updatePercent(percent);
        }
        override protected function getPreloaderView():PreloaderView
        {
            var view:PreloaderView = _preloaderView;
            _preloaderView = null;
            return view;
        }
    }
}